using Concurso;
using Curriculum.Metadatos.ClasesMetadatos;
using Curriculum.Services;

namespace ConcursoCV.UI.WebUserControls.AdmVersionesCV
{
	using System;
	using System.Data;
	using System.Drawing;
	using System.Web;
	using System.Web.UI.WebControls;
	using System.Web.UI.HtmlControls;

	/// <summary>
	///		Summary description for ABMAtributo.
	/// </summary>
	public class ABMAtributo : System.Web.UI.UserControl
	{
		protected System.Web.UI.WebControls.Label lblTitulo;
		protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
		protected System.Web.UI.WebControls.TextBox txtNombre;
		protected System.Web.UI.WebControls.RequiredFieldValidator rfvNombre;
		protected System.Web.UI.WebControls.Button btnAceptar;
		protected System.Web.UI.WebControls.DropDownList ddlTipo;
		protected System.Web.UI.WebControls.CheckBox chkOpcional;
		protected System.Web.UI.WebControls.TextBox txtLongitud;
		protected System.Web.UI.WebControls.RequiredFieldValidator rfvLongitud;
		protected System.Web.UI.WebControls.Button btnCancelar;

		private const string pidListadoDeAtributos = "011";
		private PageMode mode;
		private string atributoId = null;

		private void Page_Load(object sender, System.EventArgs e)
		{
			SetAttributesToControls();
			lblTitulo.Text = "Nuevo Atributo";

			string id = this.Request["atributoId"];
			if ( id != null ) 
			{
				atributoId = id;			
			}

			string pMode = this.Request["PageMode"];
			mode = pMode != null ? PageModeConverter.ToEnum( pMode ) : PageMode.NEW;

			
			if (!this.IsPostBack) 
			{
				this.PopulateCombos();


				if ( mode == PageMode.EDIT || mode == PageMode.VIEW ) 
				{
					CargaAtributoAControles();
				}

				if ( mode == PageMode.VIEW ) 
				{
					EnabledDisableControls(false);
				}
			}
		}

		#region - SetAttributesToControls -

		private void SetAttributesToControls()
		{
			this.txtLongitud.Attributes.Add("onKeyPress","return isNumberKey(event)");
		}

		#endregion - SetAttributesToControls -


		private void EnabledDisableControls(bool enabled)
		{
			txtNombre.Enabled = enabled;
			ddlTipo.Enabled = enabled;
			txtLongitud.Enabled = enabled;
			chkOpcional.Enabled = enabled;

			btnAceptar.Enabled = enabled;
		}

		private void PopulateCombos()
		{
			PopulateComboTipos();
		}

		private void PopulateComboTipos()
		{
			VersionMetadatosService svc = new VersionMetadatosService();			
			Helper.ListHelper.FillDropDownList(ddlTipo, "Id", "Valor", svc.GetTiposDatos(), true);
		}

		#region - Loads Methods -

		private void CargaAtributoAControles()
		{
			VersionMetadatosService svc = new VersionMetadatosService();
			
			AtributoMetadato atributoMetadato = svc.ObtenerAtributoMetadato(atributoId);
			
			txtNombre.Text = atributoMetadato.Nombre;
			txtLongitud.Text = atributoMetadato.Longitud.ToString();
			ddlTipo.SelectedValue = atributoMetadato.IdTipoDato;
			chkOpcional.Checked = atributoMetadato.Opcional;
		}

		private void CargarDatosAJurado(AtributoMetadato atributoMetadato)
		{
			atributoMetadato.Id = atributoId;
			atributoMetadato.Nombre = txtNombre.Text;
			atributoMetadato.Longitud = int.Parse(txtLongitud.Text.Trim());
			atributoMetadato.IdTipoDato = ddlTipo.SelectedValue;
			atributoMetadato.Opcional = chkOpcional.Checked;
		}

		#endregion - Loads Methods -

		private void CrearAtributo(AtributoMetadato atributoMetadato) 
		{
			VersionMetadatosService service = new VersionMetadatosService();
			atributoId = (string) service.CrearAtributoMetadato(atributoMetadato);
		}

		private void ModificarAtributo(AtributoMetadato atributoMetadato) 
		{
			VersionMetadatosService service = new VersionMetadatosService();
			atributoId = (string) service.ModificarAtributoMetadato(atributoMetadato);		
		}
		private void SaveData()
		{
			if ( mode == PageMode.VIEW ) 
			{
				btnCancelar_Click(null, null);
				return;
			}

			AtributoMetadato atributoMetadato = new AtributoMetadato();
			CargarDatosAJurado(atributoMetadato);
			
			// UPDATE
			if ( mode == PageMode.EDIT ) 
			{
				ModificarAtributo(atributoMetadato);
			}
			else
			{
				if( mode == PageMode.NEW )
				{
					// CREATE
					CrearAtributo(atributoMetadato);
				}
			}								
			RedirectToListaDeAtributos();			
		}

		private void RedirectToListaDeAtributos()
		{
			this.Server.Transfer("Default.aspx?pid=" + pidListadoDeAtributos);			
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.btnAceptar.Click += new System.EventHandler(this.btnAceptar_Click);
			this.btnCancelar.Click += new System.EventHandler(this.btnCancelar_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnAceptar_Click(object sender, System.EventArgs e)
		{
			SaveData();
		}

		private void btnCancelar_Click(object sender, System.EventArgs e)
		{
			RedirectToListaDeAtributos();
		}
	}
}
